{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 用TA_Lib做技术分析\n",
    "*TA_Lib的安装与数据格式*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 目录\n",
    "\n",
    "- 使用TA_Lib有什么好处?\n",
    "- 如何安装TA_Lib?\n",
    "- TA_Lib提供的哪些函数？\n",
    "- TA_Lib需要什么样的数据格式?\n",
    "- 如何用Tushare数据结合TA_Lib?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用TA_Lib有什么好处?\n",
    "- 提供技术分析算法的相应接口\n",
    "- 提供每个算法的说明文档\n",
    "- 提供设置参数功能"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 如何安装TA_Lib?\n",
    "- Git: https://github.com/mrjbq7/ta-lib\n",
    "- 无法安装请参考： http://act.fxdayu.com/academy/tutorials.html#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TA_Lib提供什么样的函数？\n",
    "1. 重叠研究（Overlap Studies）\n",
    "2. 动能指标（Momentum Indicators）\n",
    "3. 周期指标（Cycle Indicators）\n",
    "3. 成交量指标（Volume Indicators）\n",
    "4. 波动率指标（Volatility Indicators）\n",
    "5. 数学操作（Math Operators）\n",
    "6. 数学变换（Math Transform）\n",
    "7. 统计功能（Statistic Functions）\n",
    "8. 价格转换（Price Transform）\n",
    "9. 形态识别（Pattern Recognition）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TA_Lib需要什么样的数据格式？\n",
    "1. 'numpy.ndarray'\n",
    "2. dataframe"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 如何用Tushare数据结合TA_Lib?\n",
    "Tushare官网：http://tushare.org/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          date    open   close    high     low    volume    code\n",
      "0   2016-01-04  17.212  16.625  17.250  16.491  373405.0  600036\n",
      "1   2016-01-05  16.500  16.558  16.866  16.337  515777.0  600036\n",
      "2   2016-01-06  16.529  16.635  16.654  16.395  319826.0  600036\n",
      "3   2016-01-07  16.481  16.020  16.481  15.866  199815.0  600036\n",
      "4   2016-01-08  16.145  16.154  16.395  15.962  608227.0  600036\n",
      "5   2016-01-11  16.030  15.414  16.030  15.376  654912.0  600036\n",
      "6   2016-01-12  15.405  15.357  15.616  15.261  468282.0  600036\n",
      "7   2016-01-13  15.578  15.251  15.578  15.165  573435.0  600036\n",
      "8   2016-01-14  14.944  15.270  15.338  14.915  429630.0  600036\n",
      "9   2016-01-15  15.251  14.876  15.299  14.847  344215.0  600036\n",
      "10  2016-01-18  14.790  14.819  15.088  14.761  267350.0  600036\n",
      "11  2016-01-19  14.972  15.097  15.155  14.751  388351.0  600036\n",
      "12  2016-01-20  14.953  14.915  15.069  14.646  389306.0  600036\n",
      "13  2016-01-21  14.809  14.703  15.069  14.684  353224.0  600036\n",
      "14  2016-01-22  14.876  14.684  14.886  14.540  307008.0  600036\n",
      "15  2016-01-25  14.694  14.876  14.886  14.607  218439.0  600036\n",
      "16  2016-01-26  14.732  14.281  14.790  14.204  302085.0  600036\n",
      "17  2016-01-27  14.377  14.415  14.626  14.261  512688.0  600036\n",
      "18  2016-01-28  14.377  14.146  14.473  14.050  254779.0  600036\n",
      "19  2016-01-29  14.175  14.405  14.492  14.050  364674.0  600036\n"
     ]
    }
   ],
   "source": [
    "#Example\n",
    "import talib as ta\n",
    "import tushare as ts\n",
    "\n",
    "data = ts.get_k_data('600036', start='2016-01-01', end='2016-01-31', ktype='D',autype='qfq')\n",
    "print data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 16.625  16.558  16.635  16.02   16.154  15.414  15.357  15.251  15.27\n",
      "  14.876  14.819  15.097  14.915  14.703  14.684  14.876  14.281  14.415\n",
      "  14.146  14.405]\n",
      "<type 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "print data.close.values\n",
    "print type(data.close.values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([     nan,  16.5915,  16.5965,  16.3275,  16.087 ,  15.784 ,\n",
       "        15.3855,  15.304 ,  15.2605,  15.073 ,  14.8475,  14.958 ,\n",
       "        15.006 ,  14.809 ,  14.6935,  14.78  ,  14.5785,  14.348 ,\n",
       "        14.2805,  14.2755])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#读取'numpy.ndarray'\n",
    "ta.MA(data.close.values, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         NaN\n",
       "1     16.5915\n",
       "2     16.5965\n",
       "3     16.3275\n",
       "4     16.0870\n",
       "5     15.7840\n",
       "6     15.3855\n",
       "7     15.3040\n",
       "8     15.2605\n",
       "9     15.0730\n",
       "10    14.8475\n",
       "11    14.9580\n",
       "12    15.0060\n",
       "13    14.8090\n",
       "14    14.6935\n",
       "15    14.7800\n",
       "16    14.5785\n",
       "17    14.3480\n",
       "18    14.2805\n",
       "19    14.2755\n",
       "dtype: float64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#直接读取DataFrame,默认读取cloumns名为close的数据。\n",
    "ta.abstract.MA(data, 2)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 作业\n",
    "用talib计算多个技术指标。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
